package cn.epsilon3.service.impl;

import cn.epsilon3.common.E3BaseService;
import cn.epsilon3.constant.Base;
import cn.epsilon3.domain.e3.dto.add.E3UrlAdd;
import cn.epsilon3.domain.e3.po.E3UrlPO;
import cn.epsilon3.exception.Epsilon3ReturnException;
import cn.epsilon3.mybatis.mapper.E3UrlMapper;
import cn.epsilon3.service.E3UrlService;
import cn.epsilon3.utils.AssertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 菜单资源(e3_url)表服务接口
 *
 * @author makejava
 * @since 2023-11-12 21:49:57
 */
@Service
public class E3UrlServiceImpl extends E3BaseService implements E3UrlService {

    @Resource
    private E3UrlMapper urlMapper;

    /**
     * 新增Url
     *
     * @param urlAdd 新增Url数据
     */
    @Override
    @Transactional(rollbackFor = Epsilon3ReturnException.class)
    public void add(E3UrlAdd urlAdd) {
        E3UrlPO selectUrl = urlMapper.selectOne(queryWrappers(E3UrlPO.class)
                .eq(E3UrlPO::getUrlName, urlAdd.getUrlName())
                .or()
                .eq(E3UrlPO::getUrl, urlAdd.getUrl()));
        AssertUtils.isNotNull(selectUrl, "名称或路径已存在");

        E3UrlPO url = new E3UrlPO();
        url.setUrl(urlAdd.getUrl());
        url.setUrlName(urlAdd.getUrlName());
        url.setType(urlAdd.getType());
        url.setParentId(Base.STR0);
        url.setEffective(Base.INT0);
        AssertUtils.isTrue(Base.JDBC_TRUE == urlMapper.insert(url), "新增失败，请重试");
        getUrl().put(url.getUrl(), url.getEffective());
    }
}
